//
//  MSCommentViewSource.m
//  MSJD
//
//  Created by xiangbo on 2018/12/10.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCommentViewSource.h"
#import "MSSelfSizingCollectCell.h"
#import "MSCommentView.h"

@interface MSCommentViewSource ()<UICollectionViewDelegate,UICollectionViewDataSource,MSBasePageViewDelegate>

@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)UICollectionView *tagsView;
@property (nonatomic,strong)NSArray *tagsArray;
@property (nonatomic,assign)NSInteger pageNum;
@property (nonatomic,assign)NSInteger row;

@end

@implementation MSCommentViewSource

- (void)loadpageData:(void (^)(BOOL))completion{
    self.pageNum = 0;
    self.row = 0;
    [self.tagsView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.headView.frame = CGRectMake(0.f, 0.f, MS_SCREEN_WIDTH, self.tagsView.collectionViewLayout.collectionViewContentSize.height + 10);
        [self getCommentList:completion];
    });
}

- (void)getCommentList:(void (^)(BOOL))completion{
    _WEAKSELF
    NSDictionary *params = @{@"id":self.data.mId.stringValue ?: @"",@"page" : @(self.pageNum +1),@"size" : @(10),@"level" : @(self.row)};
    [MSRequestClient GET:@"v1/comment/list" parameters:params responseClassType:MSCommentlistResponse.class success:^(id responseObject) {
        MSCommentlistResponse *commentData = (MSCommentlistResponse *)responseObject;
        if (weakSelf.pageNum == 0) {
            [self.pageView.dataArray removeAllObjects];
        }
        [weakSelf.pageView.dataArray addObjectsFromArray:commentData.data.rows];
        if (commentData.data.rows > 0) {
            weakSelf.pageNum += 1;
        }
        [weakSelf requestResponsed:commentData.data.rows];
        
        if (completion) {
            completion(YES);
        }
    } failure:^(NSError *error) {
        [weakSelf requestResponsed:nil];
        if (completion) {
            completion(NO);
        }
    }];
}

- (void)requestResponsed:(NSArray *)dataArray{
    if (dataArray && dataArray.count == 0) {
        [self.pageView.tableview.mj_footer endRefreshingWithNoMoreData];
    }else if([self.pageView.tableview.mj_footer isRefreshing]){
        [self.pageView.tableview.mj_footer endRefreshing];
    }
    [self.pageView.tableview reloadData];
    if (!self.pageView.tableview.tableHeaderView) {
        self.pageView.tableview.tableHeaderView = self.headView;
    }
}

#pragma mark -MSDetailViewDelegate
- (NSString *)identifier:(id)data indexPath:(NSIndexPath *)indexPath{
    return NSStringFromClass(MSCommentView.class);
}

#pragma MARK --- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.tagsArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MSSelfSizingCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MSSelfSizingCollectCell.class) forIndexPath:indexPath];
    cell.textLabel.text = self.tagsArray[indexPath.row];
    cell.textLabel.textColor = indexPath.row == self.row ? UIColorHex(0xF96332):UIColorHex(0x444444);
    cell.textLabel.backgroundColor = [cell.textLabel.textColor colorWithAlphaComponent:0.1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.row = indexPath.row;
    [self.tagsView reloadData];
    self.pageNum = 0;
    [self getCommentList:nil];
}

#pragma mark - Property
- (UICollectionView *)tagsView{
    if (!_tagsView) {
        UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 20;
        layout. minimumInteritemSpacing  = 10;
        layout.estimatedItemSize = CGSizeMake(20, 32);
        layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(10,10,16,10);
        _tagsView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,58) collectionViewLayout:layout];
        _tagsView.backgroundColor = [UIColor whiteColor];
        _tagsView.delegate = self;
        _tagsView.dataSource = self;
        [_tagsView registerClass:[MSSelfSizingCollectCell class] forCellWithReuseIdentifier:NSStringFromClass(MSSelfSizingCollectCell.class)];
    }
    return _tagsView;
}

-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc] init];
        _headView.backgroundColor = UIColorHex(0xdddddd);
        [_headView addSubview:self.tagsView];
        [self.tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.headView);
            make.bottom.equalTo(self.headView).offset(-10);
        }];
    }
    return _headView;
}

- (MSCommentPageView *)pageView{
    if (!_pageView) {
        _pageView = [MSCommentPageView new];
        _pageView.delegate = self;
        [_pageView registetDetailView:NSStringFromClass(MSCommentView.class) identifier:NSStringFromClass(MSCommentView.class)];
        _WEAKSELF
        _pageView.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getCommentList:nil];
        }];
    }
    return _pageView;
}

- (NSArray *)tagsArray{
    if (!_tagsArray) {
        _tagsArray = @[@"全部",@"最新点评",@"好评",@"差评",@"带图评论"];
    }
    return _tagsArray;
}

@end
